import { createWebHistory, createRouter } from "vue-router";
import { constantRoutes } from "./routes/constantRoutes";

import { getStore } from "@/store";

const router = createRouter({
    history: createWebHistory(),
    routes: [...constantRoutes], // 静态路由
});

router.beforeEach((to, from, next) => {
    window.NLoadingBar.start();

    const useStore = getStore("user");

    if (useStore.getIsAuthenticated || to.path.indexOf("/login") !== -1) {
        if (to.matched.length === 0) {
            next("/404"); // 无匹配路由
        } else {
            next();
        }
    } else {
        next("/login"); // 未登录，跳转到登录页面
    }
});

router.afterEach(() => {
    window.NLoadingBar.finish();
});

export default router;
